package org.gitman.agentplatform.ui.actions;

import java.awt.event.ActionEvent;

import org.gitman.agentplatform.core.exceptions.NoGraphException;
import org.gitman.agentplatform.ui.api.View;
import org.gitman.agentplatform.ui.api.View.MessageType;
import org.gitman.agentplatform.ui.graph.GraphEditor;

import com.mxgraph.layout.mxOrganicLayout;
import com.mxgraph.view.mxGraph;

public class AutoLayoutAction extends UIAction{

	public AutoLayoutAction() {
		super("Autolayout Graph");
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		mxGraph graph = GraphEditor.getInstance().getGraph();
		try{
			if(graph == null){
				throw new NoGraphException();
			}else{					
				mxOrganicLayout layout = new mxOrganicLayout(graph);
				layout.setApproxNodeDimensions(false);
				
				layout.setEdgeCrossingCostFactor(8000);
				layout.setNodeDistributionCostFactor(
						layout.getNodeDistributionCostFactor() * 5);
				layout.setEdgeDistanceCostFactor(layout.getEdgeDistanceCostFactor() * 5);
				layout.setEdgeLengthCostFactor(layout.getEdgeLengthCostFactor() / 10);
				layout.execute(graph.getDefaultParent());
				
				mainFrame.getGraphPanel().fitGraphOnScreen(graph);
				
			}
		}catch (NoGraphException ex) {
			View.showMessage(ex.getMessage(), MessageType.ERROR);
		}
	}

}
